package com.micangcaihang.micang.mvp.task.ui;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.adapter.AccessoryInfoAdapter;
import com.micangcaihang.micang.databinding.FragmentExamineServiceBinding;
import com.micangcaihang.micang.entity.ServiceExmaineEntity;
import com.micangcaihang.micang.mvp.task.contract.ExamineServiceContract;
import com.micangcaihang.micang.mvp.task.presenter.ExamineServicePresenter;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/14
 *     update : 2017/8/14
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class ServiceExmaineFragment extends BaseFragment<ExamineServicePresenter,
        FragmentExamineServiceBinding> implements ExamineServiceContract.View, AccessoryInfoAdapter.OnItemClickListener {

    private AccessoryInfoAdapter mAdapter;
    private String id;
    private View mHeaderView;
    private ViewHolder mViewHolder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static ServiceExmaineFragment newInstance(String id) {
        Bundle args = new Bundle();
        ServiceExmaineFragment fragment = new ServiceExmaineFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        mHeaderView = LayoutInflater.from(_mActivity).inflate(R.layout.header_examine_service, (ViewGroup) mViewBinding.recyclerView.getParent(), false);
        mViewHolder = new ViewHolder(mHeaderView);
        mViewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter = new AccessoryInfoAdapter(_mActivity, new ArrayList<>(), false);
        mAdapter.addHeaderView(mHeaderView, 0);
        mViewBinding.recyclerView.setAdapter(mAdapter);
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_examine_service;
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, int parentPos, int position, View v) {
        MiPictureExternalPreviewActivity.externalPicturePreview(_mActivity, position,
                mAdapter.getData().get(parentPos).getFile_content(),
                StringUtils.isEmpty(mAdapter.getData().get(parentPos).getFile_name()) ? mAdapter.getData().get(parentPos).getFile_type_name() : mAdapter.getData().get(parentPos).getFile_name()
                , 1);
    }

    @Override
    public void returnExmaineService(ServiceExmaineEntity entity) {
        mAdapter.setNewData(entity.getFiles_order());
        mViewHolder.mTvOrderId.setText(String.format(_mActivity.getString(R.string.format_business_number), entity.getOrder_id()));
        mViewHolder.mTvCustomerNumber.setText(String.format(_mActivity.getString(R.string.format_client_number), entity.getOrder_id()));
        mViewHolder.mTvSalerName.setText(entity.getSaler_name());
        mViewHolder.mTvDepartmentName.setText(entity.getDepartment_name());
        mViewHolder.mTvSalerPhone.setText(entity.getSaler_phone());
        mViewHolder.mTvSalerOtherPhone.setText(entity.getSaler_other_phone());
        mViewHolder.mTvApplyMoney.setText(entity.getApply_money());
        mViewHolder.mTvStructureArea.setText(entity.getStructure_area());
        mViewHolder.mTvLocatedAddress.setText(entity.getLocated_address());
        mViewHolder.mTvChannelName.setText(entity.getChannel_name());
        mViewHolder.mTvLoanType.setText(entity.getLoan_type());
        mViewHolder.mTvCustomerName.setText(entity.getCustomer_name());
        mViewHolder.mTvMobilePhone.setText(entity.getMobile_phone());
        mViewHolder.mTvCirclePosition.setText(entity.getCircle_position());
        mViewHolder.mTvCommunityName.setText(entity.getCommunity_name());
        mViewHolder.mTvCertificateNumber.setText(entity.getCertificate_number());
        mViewHolder.mTvAveragePrice.setText(entity.getAverage_price());
        mViewHolder.mTvAverageTotal.setText(entity.getAverage_total());
        mViewHolder.mTvMortgageRate.setText(entity.getMortgage_rate());
        mViewHolder.mTvLoanCeiling.setText(entity.getLoan_ceiling());
        mViewHolder.mTvRemark.setText(entity.getRemark());
        mViewHolder.mTvShoujiaUnitPrice.setText(entity.getShoujia_unit_price());
        mViewHolder.mTvShoujiaTotalPrice.setText(entity.getShoujia_total_price());
        mViewHolder.mTvRendaUnitPrice.setText(entity.getRenda_unit_price());
        mViewHolder.mTvRendaTotalPrice.setText(entity.getRenda_total_price());

        mProgressLinearLayout.showContent();
    }


    private void setExpandDrawable(TextView view, boolean isExpand) {
        Drawable drawable;
        if (isExpand) {
            drawable = _mActivity.getResources().getDrawable(R.drawable.ic_collapse);
        } else {
            drawable = _mActivity.getResources().getDrawable(R.drawable.ic_expand);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
        view.setCompoundDrawables(null, null, drawable, null);
    }

    class ViewHolder {
        @BindView(R.id.tv_order_id)
        TextView mTvOrderId;
        @BindView(R.id.tv_customer_number)
        TextView mTvCustomerNumber;
        @BindView(R.id.tv_department_name)
        TextView mTvDepartmentName;
        @BindView(R.id.tv_saler_phone)
        TextView mTvSalerPhone;
        @BindView(R.id.tv_saler_other_phone)
        TextView mTvSalerOtherPhone;
        @BindView(R.id.expandable_layout_apply_info)
        ExpandableLayout mExpandableLayoutApplyInfo;
        @BindView(R.id.tv_loan_info)
        TextView mTvLoanInfo;
        @BindView(R.id.tv_apply_money)
        TextView mTvApplyMoney;
        @BindView(R.id.tv_structure_area)
        TextView mTvStructureArea;
        @BindView(R.id.tv_located_address)
        TextView mTvLocatedAddress;
        @BindView(R.id.tv_channel_name)
        TextView mTvChannelName;
        @BindView(R.id.expandable_layout_loan_info)
        ExpandableLayout mExpandableLayoutLoanInfo;
        @BindView(R.id.tv_loan_type)
        TextView mTvLoanType;
        @BindView(R.id.expandable_layout_product_info)
        ExpandableLayout mExpandableLayoutProductInfo;
        @BindView(R.id.tv_customer_name)
        TextView mTvCustomerName;
        @BindView(R.id.tv_mobile_phone)
        TextView mTvMobilePhone;
        @BindView(R.id.expandable_layout_borrower_info)
        ExpandableLayout mExpandableLayoutBorrowerInfo;
        @BindView(R.id.tv_circle_position)
        TextView mTvCirclePosition;
        @BindView(R.id.tv_community_name)
        TextView mTvCommunityName;
        @BindView(R.id.tv_certificate_number)
        TextView mTvCertificateNumber;
        @BindView(R.id.expandable_layout_pawn_tuft_info)
        ExpandableLayout mExpandableLayoutPawnTuftInfo;
        @BindView(R.id.tv_shoujia_unit_price)
        TextView mTvShoujiaUnitPrice;
        @BindView(R.id.tv_shoujia_total_price)
        TextView mTvShoujiaTotalPrice;
        @BindView(R.id.tv_renda_unit_price)
        TextView mTvRendaUnitPrice;
        @BindView(R.id.tv_renda_total_price)
        TextView mTvRendaTotalPrice;
        @BindView(R.id.expandable_layout_service_assess_info)
        ExpandableLayout mExpandableLayoutServiceAssessInfo;
        @BindView(R.id.tv_average_price)
        TextView mTvAveragePrice;
        @BindView(R.id.tv_average_total)
        TextView mTvAverageTotal;
        @BindView(R.id.tv_mortgage_rate)
        TextView mTvMortgageRate;
        @BindView(R.id.tv_loan_ceiling)
        TextView mTvLoanCeiling;
        @BindView(R.id.expandable_layout_service_assess_first_info)
        ExpandableLayout mExpandableLayoutServiceAssessFirstInfo;
        @BindView(R.id.tv_remark)
        TextView mTvRemark;
        @BindView(R.id.expandable_layout_remark_info)
        ExpandableLayout mExpandableLayoutRemarkInfo;
        @BindView(R.id.tv_apply_info)
        TextView mTvApplyInfo;
        @BindView(R.id.tv_product_info)
        TextView mTvProductInfo;
        @BindView(R.id.tv_borrower_info)
        TextView mTvBorrowerInfo;
        @BindView(R.id.tv_pawn_tuft_info)
        TextView mTvPawnTuftInfo;
        @BindView(R.id.tv_service_assess_info)
        TextView mTvServiceAssessInfo;
        @BindView(R.id.tv_service_assess_first_info)
        TextView mTvServiceAssessFirstInfo;
        @BindView(R.id.tv_remark_info)
        TextView mTvRemarkInfo;
        @BindView(R.id.tv_accessory_info)
        TextView mTvAccessoryInfo;
        @BindView(R.id.tv_saler_name)
        TextView mTvSalerName;

        public ViewHolder(View headerview) {
            ButterKnife.bind(this, headerview);
        }

        @OnClick({R.id.tv_apply_info, R.id.tv_loan_info, R.id.tv_product_info, R.id.tv_borrower_info, R.id.tv_pawn_tuft_info, R.id.tv_service_assess_info, R.id.tv_service_assess_first_info, R.id.tv_remark_info, R.id.tv_accessory_info})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.tv_apply_info:
                    if (mViewHolder.mExpandableLayoutApplyInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutApplyInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvApplyInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutApplyInfo.expand();
                        setExpandDrawable(mViewHolder.mTvApplyInfo, true);
                    }
                    break;
                case R.id.tv_loan_info:
                    if (mViewHolder.mExpandableLayoutLoanInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutLoanInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvLoanInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutLoanInfo.expand();
                        setExpandDrawable(mViewHolder.mTvLoanInfo, true);
                    }
                    break;
                case R.id.tv_product_info:
                    if (mViewHolder.mExpandableLayoutProductInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutProductInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvProductInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutProductInfo.expand();
                        setExpandDrawable(mViewHolder.mTvProductInfo, true);
                    }
                    break;
                case R.id.tv_borrower_info:
                    if (mViewHolder.mExpandableLayoutBorrowerInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutBorrowerInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvBorrowerInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutBorrowerInfo.expand();
                        setExpandDrawable(mViewHolder.mTvBorrowerInfo, true);
                    }
                    break;
                case R.id.tv_pawn_tuft_info:
                    if (mViewHolder.mExpandableLayoutPawnTuftInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutPawnTuftInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvPawnTuftInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutPawnTuftInfo.expand();
                        setExpandDrawable(mViewHolder.mTvPawnTuftInfo, true);
                    }
                    break;
                case R.id.tv_service_assess_info:
                    if (mViewHolder.mExpandableLayoutServiceAssessInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutServiceAssessInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvServiceAssessInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutServiceAssessInfo.expand();
                        setExpandDrawable(mViewHolder.mTvServiceAssessInfo, true);
                    }
                    break;
                case R.id.tv_service_assess_first_info:
                    if (mViewHolder.mExpandableLayoutServiceAssessFirstInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutServiceAssessFirstInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvServiceAssessFirstInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutServiceAssessFirstInfo.expand();
                        setExpandDrawable(mViewHolder.mTvServiceAssessFirstInfo, true);
                    }
                    break;
                case R.id.tv_remark_info:
                    if (mViewHolder.mExpandableLayoutRemarkInfo.isExpanded()) {
                        mViewHolder.mExpandableLayoutRemarkInfo.collapse();
                        setExpandDrawable(mViewHolder.mTvRemarkInfo, false);
                    } else {
                        mViewHolder.mExpandableLayoutRemarkInfo.expand();
                        setExpandDrawable(mViewHolder.mTvRemarkInfo, true);
                    }
                    break;
                case R.id.tv_accessory_info:
                    if (mAdapter.isExpandable()) {
                        mAdapter.setExpandable(false);
                        mAdapter.notifyDataSetChanged();
                        setExpandDrawable(mViewHolder.mTvAccessoryInfo, false);
                    } else {
                        mAdapter.setExpandable(true);
                        mAdapter.notifyDataSetChanged();
                        setExpandDrawable(mViewHolder.mTvAccessoryInfo, true);
                    }
                    break;
            }
        }
    }
}
